import org.apache.spark.sql.{DataFrame, SparkSession}

object Job1 {
  def main(args: Array[String]): Unit = {
    val spark=new SparkSession
    .Builder()
      .appName(s"${Job1.getClass.getSimpleName}")
      .master("local[*]")
      .enableHiveSupport()
      .getOrCreate()
    //加载数据
    val pdf1: DataFrame = spark.read.json("C://employee.json")
    pdf1.createOrReplaceTempView("employee")
    println("查询所有数据，并去除重复的数据:")
    pdf1.distinct().show()
    print("查询所有数据，打印时去除id字段；")
    spark.sql(
      s"""
         | select distinct e.name,e.age
         | from employee e
         | """.stripMargin).show()
    //stripMargin创建多行字符串
print("筛选出age>30的记录")
    spark.sql(
      s"""
         | select distinct e.id,e.name,e.age
         | from employee e
         | where e.age>30
         | """.stripMargin).show()
    print("查询所有记录的name列，并为其取名为username")
    print("查询年龄age的平均值。")
    spark.sql(
      s"""
         | select distinct e.id,e.name,avg(age)
         | from employee e
         | group by(name)
         | """.stripMargin).show()
  }
}
